package net.daum.hibernate.firstexample;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

public class FirstExample {
	public static void main(String[] args) {
		Session session = null;

		try {
			// This step will read hibernate.cfg.xml and prepare hibernate for
			// use
			SessionFactory sessionFactory = new Configuration().configure("net/daum/hibernate/firstexample/hibernate.cfg.xml").buildSessionFactory();
			session = sessionFactory.openSession();
			Transaction transaction = session.getTransaction();
			transaction.begin();
			// Create new instance of Contact and set values in it by reading
			// them from form object
			System.out.println("Inserting Record");
			Contact contact = new Contact();
			contact.setId(3);
			contact.setFirstName("Deepak");
			contact.setLastName("Kumar");
			contact.setEmail("deepak_38@yahoo.com");
			session.save(contact);
			System.out.println("Done");
			transaction.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			// Actual contact insertion will happen at this step
			session.flush();
			session.close();

		}

	}
}